<!-- 订单 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" clearable size="small" placeholder="请选择订单状态">
          <el-option
            v-for="item in statusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="shippingNoteNumber">
        <el-input
          v-model="queryParams.shippingNoteNumber"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否已上传" prop="isSendToPro">
        <el-select v-model="queryParams.isSendToPro" clearable size="small" placeholder="请选择">
          <el-option
            v-for="item in isSendToProOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tws:order:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tws:order:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['tws:order:query']"
          >查看</el-button>
          <el-button
            style="color: red;"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tws:order:remove']"
            v-if="scope.row.status == $store.state.constants.orderStatus_dfc && userType == $store.state.constants.userType_company"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-star-off"
            @click="handleStar(scope.row)"
            v-hasPermi="['tws:order:edit']"
            v-if="scope.row.status == $store.state.constants.orderStatus_ywc && userType == $store.state.constants.userType_company && !scope.row.starCompany"
          >评价</el-button>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="订单号" align="center" prop="shippingNoteNumber" />
      <el-table-column label="运费金额(元)" align="center" prop="totalMonetaryAmount" />
      <el-table-column label="物流公司" align="center" prop="companyName" show-overflow-tooltip v-if="userType !== $store.state.constants.userType_company"/>
      <el-table-column label="托运人" align="center" prop="consignor" />
      <el-table-column label="装货地址" align="center" prop="placeOfLoading" show-overflow-tooltip />
      <el-table-column label="收货方" align="center" prop="consignee" />
      <el-table-column label="收货地址" align="center" prop="goodsReceiptPlace" show-overflow-tooltip />
      <el-table-column label="司机" align="center" prop="driverName" />
      <el-table-column label="司机电话" align="center" prop="driverPhone" />
      <el-table-column label="是否已上传" align="center" prop="isSendToPro" :formatter="isSendToProFormat" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="95%" append-to-body :close-on-click-modal="false">
      <order-form :mode="mode" :orderId="orderId" @close="close" />
    </el-dialog>

    <!-- 评价对话框 -->
    <el-dialog title="评价" :visible.sync="starOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-form-item label="星级打分" prop="starCompany">
          <el-rate v-model="form.starCompany"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" :disabled="disabled" @click="submitStar">确 定</el-button>
        <el-button @click="starOpen = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listOrder, updateOrder, starOrder, delOrder } from "@/api/tws/order";
import OrderForm from '@/components/OrderForm/index';

export default {
  name: "Order",
  components: {
    OrderForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 评价对话框
      starOpen: false,
      disabled: false,
      userType: '',
      mode: null,
      orderId: null,
      // 是否已上传
      isSendToProOptions: [{
        dictValue: '0',
        dictLabel: '未上传'
      }, {
        dictValue: '1',
        dictLabel: '已上传'
      }],
      // 订单状态字典数据
      statusOptions: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        originalDocumentNumber: null,
        shippingNoteNumber: null,
        serialNumber: null,
        vehicleAmount: null,
        transportTypeCode: null,
        businessTypeCode: null,
        totalMonetaryAmount: null,
        policyNumber: null,
        insuranceCompanyCode: null,
        leaveTime: null,
        carNeed: null,
        remark: null,
        consignor: null,
        consignorId: null,
        loadLinkTel: null,
        placeOfLoading: null,
        countrySubdivisionCode: null,
        startLongitude: null,
        startLatitude: null,
        consignee: null,
        consigneeId: null,
        receiptLinkTel: null,
        goodsReceiptPlace: null,
        countrySubdivisionCodeS: null,
        endLongitude: null,
        endLatitude: null,
        actualCarrierName: null,
        actualCarrierBusinessLicense: null,
        actualCarrierId: null,
        driverId: null,
        driverLicense: null,
        driverName: null,
        driverPhone: null,
        vehicleId: null,
        vehicleNumber: null,
        vehiclePlateColorCode: null,
        trailerVehicleId: null,
        trailerVehiclePlateNumber: null,
        trailerVehiclePlateColorCode: null,
        despatchActualDateTime: null,
        goodsReceiptDateTime: null,
        isReceipt: null,
        backImages: null,
        starCompany: null,
        starDriver: null,
        status: null,
        isPay: null,
        isBill: null,
        isSendToPro: null,
        sendToProDateTime: null,
        tenantId: null,
        companyName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      },
      // 表单参数
      form: {},
      rules: {
        starCompany: [
          { required: true, message: "评价星级不能为空", trigger: ["blur", "change"] }
        ]
      }
    };
  },
  created() {
    this.userType = this.$store.state.user.type;
    this.getDict();
    this.getList();
  },
  methods: {
    getDict() {
      // 订单状态字典
      this.getDicts("order_status").then(response => {
        this.statusOptions = response.data;
      });
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否已上传翻译
    isSendToProFormat(row, column) {
      return this.selectDictLabel(this.isSendToProOptions, row.isSendToPro);
    },
    // 订单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    reset() {
      this.form = {
        id: null,
        starCompany: null
      }
      this.resetForm("form");
    },
    close() {
      this.open = false;
      this.mode = null;
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "修改订单";
      this.mode = null;
      const id = row.id || this.ids[0]
      setTimeout(() => {
        this.mode = '2';
        this.orderId = id;
      }, 100)
    },
    // 查看
    handleView(row) {
      this.$router.push({ path: 'orderDetail', query: { orderId: row.id }, replace: true });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该订单?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delOrder(ids);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(err => {
        this.getList();
      })
    },
    // 评价按钮
    handleStar(row) {
      this.starOpen = true;
      this.$nextTick(() => {
        this.reset();
        this.form.id = row.id;
      })
    },
    // 评价
    submitStar() {
      this.disabled = true;
      this.$refs["form"].validate(valid => {
        if(valid) {
          starOrder(this.form).then(res => {
            this.$modal.msgSuccess("已评价");
            this.disabled = false;
            this.starOpen = false;
            this.getList();
          }).catch(err =>{
            this.$modal.msgError(err.msg || "操作失败");
            this.disabled = false;
          })
        } else {
          this.disabled = false;
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tws/order/export', {
        ...this.queryParams
      }, `tws_order.xlsx`)
    }
  }
};
</script>
